using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using Newtonsoft.Json;
using System.IO;
using System;


public class Student
{
    public string Name;
    public int Index;
    public Dictionary<string, int> NameDic;
  
}
public class SampleJson : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Student sampleStudent = new Student();
        sampleStudent.Name = "zhangqi";
        sampleStudent.Index = 1;
        sampleStudent.NameDic = new Dictionary<string, int>();
        sampleStudent.NameDic.Add(sampleStudent.Name, sampleStudent.Index);
      

        /* JsonMapper.ToJson(sampleStudent);
         JsonMapper.ToObject<Student>("");
         JsonConvert.SerializeObject(sampleStudent);
         JsonConvert.DeserializeObject("");
         JsonUtility.ToJson(sampleStudent);
         JsonUtility.FromJson<Student>("");*/

        //SaveJson(sampleStudent);
        LoadJson();

    }
    void SaveJson(object targetObject)
    {
        string JsonString = JsonMapper.ToJson(targetObject);
        string JsonPath = Path.Combine(Application.dataPath,"LitJson.Sample");
        File.WriteAllText(JsonPath, JsonString);

         JsonString =JsonConvert.SerializeObject(targetObject);
         JsonPath = Path.Combine(Application.dataPath, "NewtonJson.Sample");
        File.WriteAllText(JsonPath, JsonString);

         JsonString = JsonUtility.ToJson(targetObject);
         JsonPath = Path.Combine(Application.dataPath, "JsonUtility.Sample");
         File.WriteAllText(JsonPath, JsonString);

    }
    void LoadJson()
    {
        string jsonPath = Path.Combine(Application.dataPath, "LitJson.Sample");
        string jsonString = File.ReadAllText(jsonPath);
        Student sampleStudent = JsonMapper.ToObject<Student>(jsonString);

        Debug.Log(sampleStudent.Name);

        jsonPath = Path.Combine(Application.dataPath, "NewtonJson.Sample");
        jsonString = File.ReadAllText(jsonPath);
        sampleStudent = JsonConvert.DeserializeObject<Student>(jsonString);

        Debug.Log(sampleStudent.Name);

        jsonPath = Path.Combine(Application.dataPath, "JsonUtility.Sample");
        jsonString = File.ReadAllText(jsonPath);
        sampleStudent = JsonUtility.FromJson<Student>(jsonString);

        Debug.Log(sampleStudent.Name);
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
